/**
 * @(#)com.tansun.easycare.rule.excel.handle.BaseHandle.java Copyright (c) 2017 天阳宏业科技股份有限公司
 */
package com.tansun.easycare.rule.excel.handle;

import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 处理类接口
 * @author zengdong
 * @version 1.0  2017年5月22日
 * @modified zengdong  2017年5月22日  <创建>
 */
public abstract class BaseHandle<T> {

    /**
     * Excel工作簿
     */
    protected XSSFWorkbook workbook;

    /**
     * 标题样式
     */
    protected XSSFCellStyle titleStyle;

    /**
     * 合并单元格样式
     */
    protected XSSFCellStyle mergedStyle;

    /**
     * 初始化工作空间和样式
     */
    public void initHandleTemplate() {
        this.workbook = new XSSFWorkbook();
        this.titleStyle = workbook.createCellStyle();
        this.titleStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        this.titleStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        // 设置字体
        XSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 20);
        font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
        this.titleStyle.setFont(font);
    }

    /**
     * 创建合并单元格
     * @param sheet        表格
     * @param firstRow    首行
     * @param firstCol    首列
     * @param lastRow    最后一行
     * @param lastCol    最后一列
     * @param val
     */
    public void createMergedCell(Sheet sheet, int firstRow, int firstCol, int lastRow, int lastCol, String val) {
        if (firstRow < lastRow || firstCol < lastCol) {
            CellRangeAddress cra = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
            // 在sheet里增加合并单元格
            sheet.addMergedRegion(cra);
        }
        Row row = sheet.getRow(firstRow);
        if (row == null)
            row = sheet.createRow(firstRow);
        Cell cell = row.createCell(firstCol);
        if (this.mergedStyle == null) {
            setMergedCenterStyle(true);
        }
        cell.setCellStyle(this.mergedStyle);
        cell.setCellValue(val);
    }

    /**
     * 创建合并单元格
     * @param sheet        表格
     * @param firstRow    首行
     * @param firstCol    首列
     * @param lastRow    最后一行
     * @param lastCol    最后一列
     * @param val
     */
    public void createMergedCell(XSSFWorkbook workbook,Sheet sheet, int firstRow, int firstCol, int lastRow, int lastCol, String val) {
        if (firstRow < lastRow || firstCol < lastCol) {
            CellRangeAddress cra = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
            // 在sheet里增加合并单元格
            sheet.addMergedRegion(cra);
        }
        Row row = sheet.getRow(firstRow);
        if (row == null)
            row = sheet.createRow(firstRow);
        Cell cell = row.createCell(firstCol);
        if (this.mergedStyle == null) {
            setMergedCenterStyle(workbook,true);
        }
        cell.setCellStyle(this.mergedStyle);
        cell.setCellValue(val);
    }
    /**
     * 设置默认单元格样式
     * @param isCenter
     */
    private void setMergedCenterStyle(boolean isCenter) {
        this.mergedStyle = workbook.createCellStyle();
        if (isCenter) {
            this.mergedStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
            this.mergedStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        } else {
            this.mergedStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT);
            this.mergedStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_TOP);
        }
    }
    
    /**
     * 设置默认单元格样式
     * @param isCenter
     */
    private void setMergedCenterStyle(XSSFWorkbook workbook,boolean isCenter) {
        this.mergedStyle = workbook.createCellStyle();
        if (isCenter) {
            this.mergedStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
            this.mergedStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        } else {
            this.mergedStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT);
            this.mergedStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_TOP);
        }
    }

    /**
     * 获取行
     * @param sheet
     * @param num
     * @return
     */
    protected Row getRow(Sheet sheet, int num) {
        if (sheet != null) {
            Row row = sheet.getRow(num >= 0 ? num : 0);
            if (row == null) {
                row = sheet.createRow(num >= 0 ? num : 0);
            }
            return row;
        }
        return null;
    }

    /**
     * 获取表格元素
     * @param row
     * @param num
     * @return
     */
    protected Cell getCell(Row row, int num) {
        if (row != null) {
            Cell cell = row.getCell(num >= 0 ? num : 0);
            if (cell == null) {
                cell = row.createCell(num >= 0 ? num : 0);
            }
            return cell;
        }
        return null;
    }

    /**
     * 根据坐标获取Cell
     * @param sheet
     * @param rowNum
     * @param colNum
     * @return
     */
    protected Cell getCell(Sheet sheet, int rowNum, int colNum) {
        if (sheet != null) {
            Row row = getRow(sheet, rowNum);
            Cell cell = getCell(row, colNum);
            return cell;
        }
        return null;
    }

    /**
     * Excel转Entity
     * @param wb
     */
    public abstract List<T> excelToEntity(Workbook wb, String projectCode,String userId);

    /**
     * Entity转Excel
     * @param t
     * @return
     */
    public abstract Workbook entityToExcel(List<T> list);
}
